<template>
  <div class="login-container">
    <div class="login-box to-center">
      <div class="avatar-box to-center ">
        <img src="../assets/logo.png" title="头像"  />
      </div>
      <el-form ref="loginFormRef" :model="loginForm" label-width="0px" :rules="loginRules" class="login-form">
        <el-form-item class="inp" prop="username">
          <el-input placeholder="user" v-model="loginForm.username"   prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item class="inp" prop="password">
          <el-input placeholder="password" show-password type="password" v-model="loginForm.password"  prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          {
            required: true, message: '请输入账号', trigger: 'blur'
          },
          {
            min: 3, max: 20, message: '长度在6-20之间', trigger: 'blur'
          }
        ],
        password: [
          {
            required: true, message: '请输入密码', trigger: 'blur'
          },
          {
            min: 6, max: 20, message: '长度在6-20之间', trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    resetForm() {
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: response } = await this.$http.post('login', this.loginForm)
        if (response.meta.status !== 200) return this.$message({ duration: 2000, message: '登陆失败，账号或密码错误！', type: 'error' })
        this.$message({ duration: 1000, message: '登陆成功', type: 'success' })
        sessionStorage.setItem('token', response.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped="scoped" lang="less">
  .login-container{
    height: 100%;
    width: 100%;
    background-color: #2b4b6b;
  }
  .login-box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    .avatar-box{
      top:0px;
      height: 130px;
      width: 130px;
      background-color: #ddd;
      border:5px solid #fff;
      border-radius: 50%;
      box-shadow: 0 0 10px #ddd;
      overflow: hidden;
      img{
        height: 100%;
        width: 100%;
        border-radius: 50%;
      }
    }
  }
  .login-form{
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding-right: 20px;
    padding-left: 20px;
    box-sizing: border-box;
    .inp{
      width: 100%;
    }
    .btns{
      display: flex;
      justify-content: flex-end;
    }
  }
</style>
